package com.cneiu.ppms.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.OrganizationModel;
import com.jfinal.plugin.activerecord.Page;

/**
 * 机构业务
 * @author zhaokf
 * @date 2013-04-15
 */
public class OrganizationService extends BaseService
{
	/**
	 * 将机构数据转化成Tree格式(递归实现)
	 * @param orgs
	 * @return
	 */
	public static List<HashMap<Object,Object>> treeComboBox(OrganizationModel model){
		List<HashMap<Object,Object>> smap = new ArrayList<HashMap<Object,Object>>();
		/**查询本节点下面的子节点**/
		String query_sql = "SELECT * FROM SA_ORG WHERE ORG_PARENT=?";
		List<OrganizationModel> orgs = OrganizationModel.dao.find(query_sql,model.get("ORG_NO"));
		/**组装本节点数据**/
		HashMap<Object, Object> map = new HashMap<Object, Object>();
		map.put("org_no", model.get("ORG_NO"));
		map.put("org_name", model.get("ORG_NAME"));
		map.put("treeText", model.get("ORG_NO")+" "+model.get("ORG_NAME"));
		smap.add(map);
		/**判断是否存在子节点**/
		if(orgs!=null && orgs.size()>0){
			HashMap<Object, Object> map1 =  null;
			/**循环子节点**/
			for(OrganizationModel org:orgs){
					map1 = new HashMap<Object, Object>();
					map1.put("org_no", model.get("ORG_NO"));
					map1.put("org_name", model.get("ORG_NAME"));
					map1.put("treeText", model.get("ORG_NO")+" "+model.get("ORG_NAME"));
					smap.add(map1);
					/***递归查找*/
					treeComboBox(org);
			}
		}
		return smap;
	}

	
	/**
	 * 将机构数据转化成Tree格式(递归实现)
	 * @param lines
	 * @return
	 */
	public static HashMap<Object,Object> treeList(OrganizationModel model){
		/**组装本节点数据**/
		HashMap<Object, Object> map = new HashMap<Object, Object>();
		map.put("org_no", model.get("ORG_NO"));
		map.put("org_name", model.get("ORG_NAME"));
		/**查询本节点下面的子节点**/
		String query_sql = "SELECT * FROM SA_ORG WHERE ORG_PARENT=?";
		List<OrganizationModel> orgs = OrganizationModel.dao.find(query_sql,model.get("ORG_NO"));
		List<HashMap<Object,Object>> smap = new ArrayList<HashMap<Object,Object>>();
		/**判断是否存在子节点**/
		if(orgs!=null && orgs.size()>0){
			map.put("leaf", false);
			/**循环子节点**/
			for(OrganizationModel org:orgs){
					HashMap<Object, Object> map1 = new HashMap<Object, Object>();
					map.put("org_no", model.get("ORG_NO"));
					map.put("org_name", model.get("ORG_NAME"));
					List<OrganizationModel> orgslist = OrganizationModel.dao.find(query_sql,org.get("ORG_NO"));
					if(orgslist!=null && orgslist.size()>0){
						map1.put("leaf", false);
					}else{
						map1.put("leaf", true);
					}
					smap.add(map1);
					/***递归查找*/
					treeList(org);
			}
			/**添加子节点数据**/
			map.put("children", smap);
		}else{
			map.put("leaf", true);
		}
		return map;
	}
	/**
	 * 分页列出机构列表
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<OrganizationModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("org_no") != null){
				where = " AND org_no=" + search.get("org_no");
			}
			if (search.get("org_name") != null){
				where = " AND org_name LIKE '%" + search.get("org_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}

		Page<OrganizationModel> Orgs = null;
		String select_sql = "SELECT * ";
		String query_sql = "FROM SA_ORG " + where;

		try{
			Orgs = OrganizationModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Orgs;
	}

	/**
	 * 增加机构信息
	 * @param no
	 * @param name
	 * @param type
	 * @param status
	 * @param prop
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		
		String no = params.get("org_no")==null?"":params.get("org_no").toString();
		String name = params.get("org_name")==null?"":params.get("org_name").toString();
		String type = params.get("org_type")==null?"":params.get("org_type").toString();
		String status = params.get("org_status")==null?"":params.get("org_status").toString();
		String prop = params.get("org_prop")==null?"":params.get("org_prop").toString();
		
		String sql = "select * from sa_org where org_no=?";
		OrganizationModel om = OrganizationModel.dao.findFirst(sql,no);
		if (om == null){
			om=new OrganizationModel();
			om.set("org_no", no);
			om.set("org_name", name);
			om.set("org_type", type);
			om.set("org_status", status);
			om.set("org_prop", prop);
			om.save();
			return true;
		}
		return setError("你访问的机构信息已存在！");
	}

	/**
	 * 修改机构信息
	 * @param no
	 * @param name
	 * @param type
	 * @param status
	 * @param prop
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		String no = params.get("org_no")==null?"":params.get("org_no").toString();
		String name = params.get("org_name")==null?"":params.get("org_name").toString();
		String type = params.get("org_type")==null?"":params.get("org_type").toString();
		String status = params.get("org_status")==null?"":params.get("org_status").toString();
		String prop = params.get("org_prop")==null?"":params.get("org_prop").toString();
		
		OrganizationModel om = OrganizationModel.dao.findById(no);
		if (om != null)
		{
			om.set("org_no", no);
			om.set("org_name", name);
			om.set("org_type", type);
			om.set("org_status", status);
			om.set("org_prop", prop);
			om.update();
			return true;
		}
		return setError("您访问的机构信息不存在！");
	}

	/**
	 * 删除机构信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("org_no")==null?"":params.get("org_no").toString();
		OrganizationModel om = OrganizationModel.dao.findById(no);
		if (om != null){
			om.delete();
			return true;
		}
		return setError("您访问的机构信息不存在！");
	}

}
